<script setup lang="ts">
import { onHide, onLaunch, onShow } from '@dcloudio/uni-app'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
import { configAppStore } from './store/config'
import { awaitTo } from './utils'
import { getConfig } from './service/common'

const { setConfig } = configAppStore()

onLaunch(() => {
  getConfigRequest()
})

async function getConfigRequest() {
  const { result } = await awaitTo(getConfig())
  if (result.data) {
    // result.data.refresh_interval = 2
    setConfig(result.data)
  }
}

onShow(() => {
  // #ifdef MP-WEIXIN
  updateApp()
  // #endif
  console.log('App Show')
})

// #ifdef MP-WEIXIN
const updateApp = () => {
  const updateManager = uni.getUpdateManager()

  updateManager.onCheckForUpdate(function (res) {
    // 请求完新版本信息的回调
    console.log(res.hasUpdate)
  })

  updateManager.onUpdateReady(function (res) {
    uni.showModal({
      title: '更新提示',
      content: '新版本已经准备好，是否重启应用？',
      success(res) {
        if (res.confirm) {
          // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate()
        } else if (res.cancel) {
          console.log('用户点击取消，不更新')
        }
      },
    })
  })

  updateManager.onUpdateFailed(function (res) {
    // 新的版本下载失败
    uni.showModal({
      title: '已经有新版本了哟~',
      content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
    })
  })
}
// #endif

onHide(() => {
  console.log('App Hide')
})
</script>

<style lang="scss">
/* stylelint-disable-next-line selector-type-no-unknown */
swiper,
/* stylelint-disable-next-line selector-type-no-unknown */
scroll-view {
  flex: 1;
  height: 100%;
  overflow: hidden;
}

image {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}

// 单行省略，优先使用 unocss: text-ellipsis
.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// 两行省略
.ellipsis-2 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

// 三行省略
.ellipsis-3 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

scroll-view::-webkit-scrollbar {
  height: 16rpx;
}

scroll-view::-webkit-scrollbar-thumb {
  background-color: #888888;
  border-radius: 10rpx;
}

scroll-view::-webkit-scrollbar-track {
  background-color: #f0f0f0;
  border-radius: 10rpx;
}
</style>
